package com.project.demo.controller;

import com.project.demo.entity.CateringOrders;
import com.project.demo.entity.HomestayOrders;
import com.project.demo.service.CateringOrdersService;
import com.project.demo.controller.base.BaseController;
import com.project.demo.service.HomestayOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.project.demo.service.base.AliPayService;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;


/**
 * 餐饮订单：(CateringOrders)表控制层
 *
 */
@RestController
@RequestMapping("/catering_orders")
public class CateringOrdersController extends BaseController<CateringOrders, CateringOrdersService> {

    /**
     * 餐饮订单对象
     */
    @Autowired
    public CateringOrdersController(CateringOrdersService service) {
        setService(service);
    }


    @Autowired
    AliPayService aliPayService;
    @Autowired
    HomestayOrdersService homestayOrdersService;

    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String,Object> paramMap = service.readBody(request.getReader());
        List<HomestayOrders> homestayOrders = homestayOrdersService.selectByUserId(Integer.valueOf(paramMap.get("ordinary_users")+""));
        if(homestayOrders.isEmpty()){
            return error(0,"您未住，不能下单");
        }
        this.addMap(paramMap);
        return success(1);
    }


    /**
     * 沙箱支付
     */
    @GetMapping("/alipay")
    public Map<String, Object> alipay(String goods_name,double pay_money) {
        return success(aliPayService.getQrCode(goods_name,pay_money));
    }
}
